/*
 * Copyright (c) 2013.
 *  游戏服务器核心代码编写人陈磊拥有使用权
 *  联系方式：E-mail:13638363871@163.com ;qq:502959937
 *  个人博客主页：http://my.oschina.net/chenleijava
 */

package com.dc.gameserver.serverCore.controller.handleController.itemsController;

import com.dc.gameserver.baseConfig.Config;
import com.dc.gameserver.proto.itemserverprotoVo.achiveLingQiRequest;
import com.dc.gameserver.proto.itemserverprotoVo.achiveLingQiResponse;
import com.dc.gameserver.proto.itemserverprotoVo.roleVo;
import com.dc.gameserver.serverCore.controller.AbstractController.AbstractController;
import com.dc.gameserver.serverCore.controller.AbstractController.IController;
import com.dc.gameserver.serverCore.model.dbsql.modelCache.ModelCacheService;
import com.dc.gameserver.serverCore.model.dbsql.modelService.PlayerService;
import com.dc.gameserver.serverCore.model.dbsql.modelVo.TRole;
import com.dc.gameserver.serverCore.model.dbsql.modelVo.TSprite;
import com.dc.gameserver.serverCore.model.gamemodel.StateModel;
import com.dc.gameserver.serverCore.model.gamemodel.script.ScriptFactory;
import com.dc.gameserver.serverCore.service.battle.GameFormula;
import com.dc.gameserver.serverCore.service.character.PlayerInstance;
import com.google.protobuf.MessageLite;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 石头哥哥 </br>
 *         dcserver1.3 </br>
 *         Date:14-1-13 </br>
 *         Time:上午10:45 </br>
 *         Package:{@link com.dc.gameserver.serverCore.controller.handleController.itemsController}</br>
 *         Comment： 领取灵气值  ，更换 灵泉 灵脉      时间戳的更改
 */
@Service
public class getLingQiController extends AbstractController {

    @Resource
    private ModelCacheService modelCacheService;

    @Resource
    private PlayerService playerService;

    @Override
    public void PostConstruct() {
        int ID= achiveLingQiRequest.msgID.ID_VALUE;
        GAME_CONTROLLERS[ID]=this;
        IController.MESSAGE_LITE[ID]=achiveLingQiRequest.getDefaultInstance();
    }

    /**
         message  achiveLingQiResponse{
         enum msgID {
         ID = 2018;
         }
         required uint32 LingQi=1;//角色当前的灵气
         optional uint32  battleHeart=2;//是否可以参加心魔战斗  1.是 ；2.还没有到达要求
         optional stateUpgrade stateUp=3;// 境界提升
         }
     * @param messageLite     数据载体
     * @param player active object
     * @throws Exception
     * 领取， 计算当前灵气存储值
     */
    @Override
    @SuppressWarnings("unchecked")
    public void DispatchMessageLit(MessageLite messageLite, PlayerInstance player) throws Exception {
        //防止频繁点击领取灵气
        long currentTime=System.currentTimeMillis();
        if (currentTime-player.getAchiveTime()<= Config.DEFAULT_VALUE.GAME_VALUE.getSpriteInitial){
            return;
        }
        player.setAchiveTime(currentTime);

        int roleID=player.getID();
        String role_prefix=ModelCacheService.ROLE_PREFIX(roleID);
        TRole tRole= (TRole) modelCacheService.findByPrefixID(role_prefix);


        if (checkSprite(player,tRole)) spriteCompute(player,tRole,currentTime,modelCacheService,playerService, (List<TSprite>) modelCacheService.findListByPrefixID(ModelCacheService.SPRITE_PREFIX(roleID)),true);

        //点击领取 ，然后判定领取的量是否足够提升境界；
        int roleSpriteExp=tRole.getRolestateexperies()+tRole.getStorelingqi();//角色当前的灵气（经验）
        /**
         * 玩家小境界提升逻辑处理
         */
        StateModel roleState=ScriptFactory.stateModelFastMap.get(tRole.getStateid());//境界唯一编号
        int stateID=roleState.getId();
        int spiritRequired=roleState.getSpiritRequired();
        achiveLingQiResponse.Builder builder=achiveLingQiResponse.newBuilder();
        builder.setBattleHeart(0x2);  // default value  not  battle hear ！
        if (roleSpriteExp>=spiritRequired){

            tRole.setStorelingqi(roleSpriteExp-spiritRequired);//当前储存量

            tRole.setRolestateexperies(0x0);
            StateModel nextState=ScriptFactory.stateModelFastMap.get(stateID+ 0x1);
            int nextSmallStateID=nextState.getSmallStateID();
            //到达小境界10，完成大境界中的境界提升，准备心魔战斗，完毕之后，更新大境界编号 小境界编号
            if (nextSmallStateID== 0xa){
                builder.setBattleHeart(0x1);
            }
            tRole.setStateid(nextState.getId()); //下一个境界唯一编号
            tRole.setRolesmalstate(nextState.getSmallStateID());  //提升小境界


            //组装数据
            int blood = GameFormula.Blood(nextState.getBlood(), 0, 0, 0, 0);
            int magic=GameFormula.Magic(nextState.getMagic(), 0, 0, 0, 0);
            int recoveryMagic= GameFormula.recoveryMagicForPreRound(nextState.getRolerecoverymagic(),0,0);
            int sheild=GameFormula.Sheild(nextState.getSheild(), 0, 0, 0, 0);
            int attack=GameFormula.Attack(nextState.getAttack(), 0, 0, 0, 0);
            int defence=GameFormula.Defence(nextState.getDefence(), 0, 0, 0, 0);
            int maxSoul=GameFormula.MaxSoual(nextState.getSoul(), 0, 0, 0, 0); //神识上限


            tRole.setRolemaxblood(blood+tRole.getRolemaxblood());
            tRole.setRolemaxmagic(magic+tRole.getRolemaxmagic());
            tRole.setRolerecoverymagic(recoveryMagic+tRole.getRolerecoverymagic()); //法力值恢复
            tRole.setRolemaxshiled(sheild+tRole.getRolemaxshiled());
            tRole.setRoleattack(attack+tRole.getRoleattack());
            tRole.setRoledefence(defence+tRole.getRoledefence());
            tRole.setRolesual(nextState.getSoul()+tRole.getRolesual());
            tRole.setRolemaxsual(maxSoul+tRole.getRolemaxsual()); //神识上限值
            tRole.setRolesualrecovery(nextState.getRoleSualRecovery()+tRole.getRolesualrecovery());//神识恢复速度
            tRole.setRolesmalstate(nextState.getSmallStateID()); //小境界编号
            tRole.setRolemaxstate(nextState.getBigStateID());  //大境界编号
            tRole.setRolebaseminmomentum(nextState.getBaseminMomentum()); //基础气势值上限
            tRole.setRolebasemaxmomentum(nextState.getBasemaxMomentum()); // 基础气势值下限




            roleVo.Builder _roleVo=roleVo.newBuilder();
            _roleVo.setRoleID(tRole.getRoleid());
            _roleVo.setRoleMaxBlood(tRole.getRolemaxblood());
            _roleVo.setRoleMaxMagic(tRole.getRolemaxmagic());
            _roleVo.setRoleRecoveryMagic(tRole.getRolerecoverymagic());
            _roleVo.setRoleMaxShiled(tRole.getRolemaxshiled());
            _roleVo.setRoleAttack(tRole.getRoleattack());
            _roleVo.setRoleDefence(tRole.getRoledefence());
            _roleVo.setRoleBaseMinMomentum(tRole.getRolebaseminmomentum());
            _roleVo.setRoleBaseMaxMomentum(tRole.getRolebasemaxmomentum());
            _roleVo.setRoleMaxSual(tRole.getRolemaxsual());
            _roleVo.setRoleSualRecovery(tRole.getRolesualrecovery());
            _roleVo.setRoleExperiences(tRole.getRoleexperiences());
            _roleVo.setRoleLingshiNum(tRole.getRolelevel());
            _roleVo.setRoleMurder(tRole.getRolemurder());
            _roleVo.setRoleMurderLevel(tRole.getRolemurderlevel());
            _roleVo.setRoleLuck(tRole.getRoleluck());
            _roleVo.setRoleLingshiNum(tRole.getRolelingshinum());
            _roleVo.setRoleXianYuNum(tRole.getRolexianyunum());
            _roleVo.setRoleLevel(tRole.getRolelevel());

            _roleVo.setRoleSoual(tRole.getRolesual()); //角色当前神识值
            _roleVo.setRoleFiveType(tRole.getRolefivetype()); //角色五行属性

            //境界 提升
            _roleVo.setRoleSmalState(tRole.getRolesmalstate());//角色小境界
            _roleVo.setRoleMaxState(tRole.getRolemaxstate());//角色大境界

            _roleVo.setRoleLingQi(tRole.getRolestateexperies());//角色当前灵气
            _roleVo.setRoleLingQILimit(tRole.getStorelingqilimit());//角色灵气存储上限

            double minCount= (tRole.getRolebaseminmomentum()+tRole.getRoleexperiences()/Math.pow(0xa,tRole.getRolesmalstate()));
            double maxCount=(tRole.getRolebasemaxmomentum()+tRole.getRolemurder()*(0xa-(tRole.getRolemaxstate()- 0x1))/Math.pow(0x64,tRole.getRolemaxstate()));

            //气势值---产生点数
            _roleVo.setRoleMaxMomentum((int) maxCount);
            _roleVo.setRoleMinMomentum((int)minCount);

            builder.setRoleVo(_roleVo.build());
        } else {
            //没有境界提升，领取完毕
            tRole.setStorelingqi(0x0);//领完
            tRole.setRolestateexperies(roleSpriteExp);
        }

        builder.setLingQi(tRole.getRolestateexperies());
        builder.setLingQiInBook(tRole.getStorelingqi());     //当前存储的灵气值

        //update  and add to cache
        playerService.updateRole(tRole);
        modelCacheService.addObjectPrefixID(role_prefix, tRole);
        player.DirectSend(wrappedBufferShort(achiveLingQiResponse.msgID.ID_VALUE, builder.build()));

    }
}
